package shoppingCartFrame;

import java.awt.BorderLayout;
import java.util.*;

import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;

import shoppingCart.*;

public class ShoppingCartPanel extends JPanel implements Observer{

	protected ShoppingCart cart;
	protected ObjectList itemList;
	protected JTextField customerName;
	protected JTextField totalField;
	
	private JButton removeItem = new JButton("Remove Item");
	private JButton placeOrder = new JButton("Place Order");
	
	public ShoppingCartPanel(){
		
		// Make this class an observer of the shopping cart
		cart = new ShoppingCart();
		cart.addObserver(this);

// Create the list of objects in the cart
		itemList = new ObjectList();

// Create the field for the total cost so far
		totalField = new JTextField(10);
		totalField.setEditable(false);
		totalField.setText("Total: "+cart.total);

		setLayout(new BorderLayout());

// Create a field for the customer name
		customerName = new JTextField(20);

// Combine the label and the name field on a single panel
		JPanel namePanel = new JPanel();
		namePanel.add(new JLabel("Customer Name: "));
		namePanel.add(customerName);

// Put the name field up at the top and the item list in the center
		add("North", namePanel);
		add("Center", itemList);

// Create buttons for removing items and placing an order and put
// them along the bottom.

		JPanel southPanel = new JPanel();
		southPanel.add(removeItem);
		removeItem.addActionListener(new ActionListener (){
			public void actionPerformed(ActionEvent e){
				doRemove();
			}
			});
		
		southPanel.add(placeOrder);
		placeOrder.addActionListener(new ActionListener (){
			public void actionPerformed(ActionEvent e){
				doPlaceOrder();
			}
			});
		
		southPanel.add(totalField);

		add("South", southPanel);
		
		setBorder(BorderFactory.createTitledBorder("Shopping cart:"));

// Tell the frame registry about this frame
		PanelRegistry.instance().addPanel("Shopping Cart", this);
		
	}
	
	public String makeItemString(ShoppingCartItem item)
	{
		String itemString = ""+ item.itemName + "" + "Qty: " + item.quantity + 
				"\t" + "Cost: " + item.itemCost;
		return itemString;
	}
	
	@SuppressWarnings("deprecation")
	public void update(Observable whichCart, Object ob)
	{
		ShoppingCartEvent event = (ShoppingCartEvent) ob;

		if (event.eventType == ShoppingCartEvent.ADDED_ITEM) {
// If there is a new item in the cart, add it to the scrollable list
			itemList.addObject(makeItemString(event.item),
				event.item);
			totalField.setText("Total: "+cart.total);
			itemList.validate();
		} else if (event.eventType ==
// If an item has been removed from the cart, remove it from the list
			ShoppingCartEvent.REMOVED_ITEM) {
			itemList.delObject(event.item);
			totalField.setText("Total: "+cart.total);
			itemList.validate();
		} else if (event.eventType ==
			ShoppingCartEvent.CHANGED_ITEM) {
// If an item has changed, update the list
			int index = itemList.indexOf(event.item);
			itemList.replaceObject(makeItemString(
				event.item), event.item, index);
			totalField.setText("Total: "+cart.total);
			itemList.validate();
		}
	}
	
	// If the user clicks on "Remove Item," remove it from he list
			public void doRemove()
			{
				Object ob = itemList.getSelectedObject();
				if (ob == null) return;

				ShoppingCartItem item = ((ShoppingCartItem)ob).copy();
				item.quantity = 1;
				cart.removeItem(item);
			}
			
			// doPlaceOrder uses PostSockURL to post the order to a web
			// server. You will need to customize this method to fit your needs.

				public void doPlaceOrder()
				{
					
				cart.resetShoppingCart();
				itemList.deleteAllObjects();
				Object ob = itemList.getSelectedObject();
				if (ob == null) return;
					//cart = new ShoppingCart();
					//itemList = new ObjectList();
					//cart.resetShoppingCart(cart);
				/*	try {
						URL postURL = new URL(
							getDocumentBase().getProtocol(),
							getDocumentBase().getHost(),
							getDocumentBase().getPort(),
							"/shopping");

						ByteArrayOutputStream byteOut = 
							new ByteArrayOutputStream();
						PrintStream outStream = 
							new PrintStream(byteOut);

						outStream.println("Custname: "+
							customerName.getText());
						ShoppingCartItem[] items = cart.getItems();
						for (int i=0; i < items.length; i++) {
							outStream.println(
							items[i].itemName+"|"+
							items[i].quantity);
						}

						String response = PostSockURL.post(postURL,
							byteOut.toString());
						System.out.println(response);
					} catch (Exception e) {
						e.printStackTrace();
					}
						*/
				}

				public ShoppingCart getShoppingCart()
				{
					return cart;
				}

}
